<?php 

import('@.com.99ebc.lib.Error');
import('@.com.99ebc.lib.File');
import('@.com.99ebc.lib.Xml');

class MessageModel extends Model {
    //表字段配置
    protected $fields   = array(
                                'message_id',
                                'message_name',
                                'message_desc',
                                'message_email',
                                'message_phone',
                                'edit_time',
                                '_pk' => 'message_id',
                                '_autoinc' => true
                            );
    
    //自动验证
    protected $_validate    = array();

    //自动完成
    protected $_auto        = array(
        array('edit_time', 'mktime', 3, 'function'),
    );

    protected static $orderFields = 'edit_time DESC';

    protected $_countWhere;

    protected $_error;

    public function __construct() {
        parent::__construct();

        $this->_countWhere  = '';
        $this->_error       = new Error(); 
    }

    //得到记录总数
    public function getCountRecords() {
        return $this->where($this->_countWhere)->count();
    }
    
    //搜索方法
    public function searchMessage($page = 0, $keyword, $perPage = 10) {
        if(preg_match('/^\/+\d+$/', $keyword)) {
            $whereSearch  = '`message_id` = ' . strtr($keyword, array('/' => ''));
        } else {
            $whereSearch  = '`message_name` like \'%' . $keyword . '%\'';
        }
        $this->_countWhere  = $whereSearch;

        return $this->seeMessage($page, $whereSearch, $perPage);
    }

    //查看列表方法 
    public function seeMessage($page = 0, $whereIndex = '', $perPage = 10) {
        return $this->where($whereMessage)->order(self::$orderFields)->limit($perPage)->page($page)->select();
    }

    //查看记录详细信息方法
    public function infoMessage($recordId) {
        $record     = $this->where('`message_id` = ' . $recordId)->find();
        return $record;
    }

    //编辑记录方法
    public function editMessage($recordId) {
        return false;
        if(empty($_POST['edit_password'])) {
            $data['message_id']  = $_POST['message_id'];
            $data['message_name']  = $_POST['message_name'];
            $data['sort_loc']   = $_POST['sort_loc'];
            $data['message_desc']  = $_POST['message_desc'];
            $data['image_path']  = $_POST['image_path'];
        } else {
            $data   = $this->create();
            if(false === $data) {
                return $this->getError();
            }
        }

        if(false === $this->save($data)) {
            return $this->getError();
        }

        return true;
    }

    //添加记录方法
    public function addMessage($data = null) {
        if(empty($data)) {
            if(false === $this->create()) {
                Logger::log(var_export($_POST, true));
                return $this->getError();
            }
        } else {
            $this->data     = $data;
        }
        if(false === $this->add()) {
            return $this->getError();
        }

        return true;
    }

    //删除记录方法
    public function deleteMessage($recordId) {
        return $this->quickDeleteMessage(array($recordId));
    }

    //快捷操作：删除记录
    public function quickDeleteMessage(array $recordIds){
        $whereStr   = $this->genWhereIn('message_id', $recordIds);
        
        return $this->where($whereStr)->delete();
    }

    //快捷操作：删除记录相关文件
    public function quickDeleteImageMessage(array $recordIds) {
        $whereStr   = $this->genWhereIn('message_id', $recordIds) . ' AND (`image_path` !=\'\' AND `image_path` IS NOT NULL)';
        $recordList = $this->where($whereStr)->field('message_id, image_path')->select();
        if(empty($recordList)) {
            return ture;
        }
        $whereStr   = '`message_id` IN (';
        foreach($recordList as $record) {
            if($this->deleteFile($record['image_path'])) {
                $whereStr   .= $record['message_id'] . ', ';
            }
        }
        $whereStr    .= '-1)'; 

        return $this->where($whereStr)->setField('image_path', '');
    }
    
    //生成WhereIn条件语句
    protected function genWhereIn($field, array $values) {
        return '`' . $field . '` IN (' . implode(',', $values) . ', -1)';
    }

    public function getUploadDir() {
        return '';
    }
}

?>
